Skip to content

loom code backfill keeps an owed edge pass across a resume - #1583

Open
KaloyanNaumov wants to merge 2 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/backfill-owed-edge-pass
Open

KaloyanNaumov wants to merge 2 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/backfill-owed-edge-pass

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Related: #1540, #1552

TL;DR: A first full backfill interrupted before its edge pass finishes without that pass when it is resumed, so the namespace's cross-file references stay unbound and nothing says so. The run now records that it owes the pass before its first batch and clears the record once the pass finishes without errors, so a resumed run, or a rerun after an interrupted pass or one that ended with a failed file write, runs it.

Changelog category (leave one):

  • Bug Fix

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

A first loom code backfill that is interrupted (a held org, a SIGTERM, a crash) now runs its edge second pass when it is resumed, instead of finishing with its cross-file references unbound.

Why

A namespace's first full backfill implies the edges-only second pass, because files are indexed in tree order: a call into a file later in the tree binds nothing until the pass re-derives it. second_pass_wanted implied the pass only while the cursor was empty. Two runs lost it:

  1. A first backfill stopped in its index phase resumes with the cursor set.
  2. A rerun after a stop during the edge pass finds the cursor at its end.

Both write git_head and finish.

What changes

  • The run records why it owes the pass under backfill_edges_owed:index<suffix> in code_index_state before its first batch. The reason is first-backfill, or requested for --edges-second-pass. The record is cleared once the pass finishes without errors; a pass that reports a failed file write leaves it owed, and the next run repeats the pass.
  • A later cursor run reads the record and runs the pass.
  • --no-edges-second-pass declines the pass and clears the record.
  • A --paths-file repair records nothing, as before.
  • second_pass_wanted becomes edge_pass_owed, which returns the reason. _record_edge_pass reads and writes the record; it lives outside _backfill, which is at the function-length limit.

Scope and risk

  • Behaviour change for every one-process user: a resumed first backfill, and a rerun after an interrupted edge pass, now run a full-tree edges-only pass where main finished without one. --edges-second-pass survives an interruption.
  • One new state row per namespace. It shows in the full code.index_status answer.
  • The new key is outside the counts and coverage fingerprints (_COUNTS_KEYS, _FINGERPRINT_KEYS). Every write of it comes before the phase's republish, so the published rows stay current.
  • Erasure: code_index.purge deletes every code_index_state row of the namespace.
  • The cursor key is shared with the server's own first index (bootstrap.CURSOR_KEY). A record a CLI run leaves is discharged by the next CLI run, which is at most one extra pass.

Compatibility, rollout and rollback

  • Wire: none. Setting: none. Schema: none.
  • Rolling upgrade: a build without this change ignores the row and drops the pass on its resume, as before. A record it leaves behind is discharged by this build's next cursor run.
  • Rollback: revert. The leftover rows are inert and are purged with their namespace.

Tests

tests/unit/test_cli_backfill_second_pass.py runs _backfill over a stubbed store and checkout:

  • test_a_first_backfill_interrupted_in_its_index_phase_gets_its_edge_pass_on_resume
  • test_a_rerun_after_an_interrupted_edge_pass_runs_the_pass
  • test_declining_the_pass_clears_what_an_earlier_run_owed
  • test_a_resumed_run_runs_the_pass_an_earlier_run_owed
  • test_an_edge_pass_with_a_failed_file_write_stays_owed_until_a_rerun_finishes_clean
  • the three existing rule tests, now on edge_pass_owed

The four _backfill tests fail on main (no pass on resume). The tests that import code_backfill (13 files), plus test_exception_text_ratchet, test_tunable_constants and test_public_copy, pass in CI's environment (Python 3.12, uv sync --locked --extra dev --extra code). So do ruff, mypy on server/cli/code_backfill.py, and the function-length gate.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code wrote the diff and the tests. Checked: I reviewed the whole diff and its tests; the test and gate runs listed under Tests are the agent's.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

How this was found

Found while reviewing loom code backfill --workers (#1552), whose workers depend on this pass. Fixed on a fork, PR submitted.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: none are affected.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (No migration.)
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version. (No plugin file.)
  • The frontend builds and its tests pass, if frontend/ changed. (Unchanged.)
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

Note

Medium Risk
Changes default behavior for resumed or rerun backfills (may add a full-tree edge pass) and introduces durable per-namespace state, but scope is limited to the CLI backfill index phase with no schema or wire changes.

Overview
Fixes a resume bug where a first full loom code backfill could finish without the edges-only second pass after an interrupt, leaving cross-file references unbound. The pass was only implied while the index cursor was still empty, so a mid-index resume (cursor set) or a rerun after a crash during the edge pass would skip it and still advance git_head.

The index phase now persists backfill_edges_owed:index<suffix> in code_index_state: _record_edge_pass writes the reason (first-backfill or requested) before the first batch, edge_pass_owed (replacing second_pass_wanted) decides whether the current run must run the pass (including honoring a stored recorded value), and the key is cleared only after a successful edge pass. --no-edges-second-pass declines and clears the record; --paths-file repairs still record nothing. Operator docs describe the record/clear lifecycle.

Unit tests expand test_cli_backfill_second_pass.py with stubbed _backfill runs for interrupt/resume, interrupted edge pass, declining the pass, and edge-pass errors.

Reviewed by Cursor Bugbot for commit 948904c. Bugbot is set up for automated code reviews on this repo. Configure here.

A first full backfill implies the edges-only second pass, but only while
its cursor is empty. A first backfill stopped in its index phase (a held
org, a SIGTERM, a crash) resumes with the cursor set, and a rerun after an
interrupted edge pass finds the cursor at its end: both finish, write
`git_head` and drop the pass, so the namespace's cross-file references
stay unbound and nothing says so.

The run now records why it owes the pass (`first-backfill`, or
`requested` for `--edges-second-pass`) under
`backfill_edges_owed:index<suffix>` before its first batch, and clears the
record once the pass is done. A resumed run or a rerun reads the record and
runs the pass; `--no-edges-second-pass` declines it and clears the record.
A `--paths-file` repair records nothing, as before.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@vercel

vercel Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Nov 26, 2026 9:05pm UTC
loom-telemetry Ready Ready Preview Nov 26, 2026 9:05pm UTC

Request Review

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The persisted record survives cancellation. A partial pass also needs to leave work owed.

Comment thread server/cli/code_backfill.py Resolved
@KaloyanNaumov
KaloyanNaumov marked this pull request as draft November 26, 2026 20:49
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Converting to draft while I address it; a new head and the replies will follow, so no need to look at this head again.

The second edge pass returns per-file write failures in its error
count, yet the owed record was cleared whenever the pass returned.
A file whose edge write failed then never got another pass: the
record was gone and the run published its watermark.

Clear the record only when the pass reports no errors; a rerun
repeats it. The new test fails one file's edge write, checks the
record stays owed, and shows a clean rerun running the pass again
and clearing it.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@KaloyanNaumov
KaloyanNaumov marked this pull request as ready for review November 26, 2026 21:03

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An edge pass with a per-file write error leaves the owed record in place. The regression verifies that a rerun executes the pass again and clears the record after a clean result.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants